<template>
	<view class="mine-wrap">
		<view class="top-backgroup">
			<image src='../../static/imgs/bg1.png' class='backgroud-image'></image>
			<view class="mine-user-info">
				<view class="top-space"></view>
				<view class="mine-user-bar">
					<view class="mine-user-bar-avatar">
						<image src="../../static/imgs/avatar1.png"></image>
					</view>
					<view class="mine-user-bar-name">
						<view @click="handleOpenUpdateUser" class="mine-user-bar-nick">{{ userInfo && userInfo.loginStatus ? (userInfo.nickname || '微信用户') : '未登录，点击登录' }}</view>
						<view class="mine-user-bar-uid">
							<text v-if="userInfo && userInfo.loginStatus">积分：{{ userInfo.activePoints || '0' }}</text>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="mine-base-style mine-menus">
			<view class="mine-menus-item" @click="goPage(0)">
				<view class="mine-menus-item-title">积分明细</view>
				<view class="mine-menus-item-icon">
					<image src="../../static/imgs/right.png"></image>
				</view>
			</view>
			<view class="mine-menus-item" @click="goPage(1)">
				<view class="mine-menus-item-title">获取积分</view>
				<view class="mine-menus-item-icon">
					<image src="../../static/imgs/right.png"></image>
				</view>
			</view>
			<view class="mine-menus-item" @click="goPage(2)">
				<view class="mine-menus-item-title">积分兑换</view>
				<view class="mine-menus-item-icon">
					<image src="../../static/imgs/right.png"></image>
				</view>
			</view>
			<view class="mine-menus-item" @click="goPage(4)">
				<view class="mine-menus-item-title">我的兑换</view>
				<view class="mine-menus-item-icon">
					<image src="../../static/imgs/right.png"></image>
				</view>
			</view>
			<view class="mine-menus-item" @click="goPage(5)">
				<view class="mine-menus-item-title">复制UID</view>
				<view class="mine-menus-item-icon">
					<image src="../../static/imgs/right.png"></image>
				</view>
			</view>
		</view>
		<view class="mine-base-style mine-gzh-adv">
			<view class="mine-gzh-code">
				<image src="../../static/imgs/gzh.jpg"></image>
			</view>
			<view class="mine-gzh-title">
				<view>关注“天晴软件”微信公众号，获取更多软件资讯！</view>
			</view>
		</view>
		<view v-if="showAdv" class="mine-base-style mine-gzh-adv">
			<!-- 广告位 广告注册后放开注释 -->
			<!-- <ad adpid="1636300520" @load="onLoadBannerAdv" @close="onCloseBannerAdv" @error="onErrorBannerAdv"></ad> -->
		</view>
		<!-- 弹窗 -->
		<uni-popup ref="updateDialog" type="dialog" :mask-click="false">
			<view class="mine-update-user-model">
				<image @click="handleCloseUpdate" class="update-model-close" src="../../static/imgs/close.png"></image>
				<view class="mine-update-title">
					修改个人信息
				</view>
				<view class="mine-update-body">
					<view class="uni-form-item">
						<view>昵称：</view>
						<input v-model="upUser.nickname" class="uni-input" placeholder="请输入昵称" />
					</view>
				</view>
				<view class="mine-update-btns">
					<button @click="submitUpdateUserInfo" style="margin: 0;" type="primary" size="mini">确认</button>
					<button @click="handleCloseUpdate" style="margin: 0 20rpx;" type="default" size="mini">取消</button>
				</view>
			</view>
		</uni-popup>
		<!-- 登录框 -->
		<uni-popup ref="loginDialog" type="dialog" :mask-click="false">
			<view class="mine-login-model">
				<image @click="handleCloseLogin" class="login-model-close" src="../../static/imgs/close.png"></image>
				<image src="../../static/imgs/wechat.png"></image>
				<button type="primary" @click="login">微信一键登录</button>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import apis from '../../apis/apis.js';
	
	export default {
		data() {
			return {
				userInfo: {
					activePoints: '1250',
					loginStatus: false,
				},
				openLogin: false,
				upUser: {
					nickname: '',
				},
				showAdv: false,
				errorLog: '',
			};
		},
		onShow() {
			this.initUserData();
		},
		methods: {
			handleOpenUpdateUser() {
				const token = uni.getStorageSync('token');
				if (token && this.userInfo.loginStatus) {
					this.upUser.nickname = this.userInfo.nickname;
					this.$refs.updateDialog.open();
				} else {
					this.$refs.loginDialog.open();
				}
			},
			copyCode(data) {
				if (data) {
					uni.setClipboardData({
						data: data,
						success: function() {
							console.log('success');
						}
					});
				}
			},
			login() {
				const that = this;
				uni.login({
					success(res) {
						if (res.code) {
							//发起网络请求
							that.$http.post({
								url: apis.loginUrl,
								data: {
									code: res.code
								},
							}).then(res => {
								if (res.code === 200) {
									uni.setStorageSync('token', res.token);
									uni.showToast({
										title: '登录成功',
										duration: 2000
									});
									that.getUserInfo();
									that.handleCloseLogin();
								}
							}).catch(err => {
								console.log('调用接口失败：', err);
							});
						} else {
							console.log('登录失败！' + res.errMsg)
						}
					},
					fail(err) {
						console.log(err);
					}
				})
			},
			getUserInfo() {
				this.$http.get({
					url: apis.userInfoUrl
				}).then(res => {
					console.log(res, '调用接口成功');
					res.data.loginStatus = true;
					uni.setStorageSync('userInfo', res.data);
					this.initUserData();
				}).catch(err => {
					console.log('调用接口失败：', err);
				})
			},
			handleCloseLogin() {
				this.$refs.loginDialog.close();
			},
			handleCloseUpdate() {
				this.$refs.updateDialog.close();
			},
			initUserData() {
				// 初始化用户数据
				const userInfo = uni.getStorageSync('userInfo');
				const token = uni.getStorageSync('token');
				if (token && userInfo && userInfo.loginStatus) {
					this.userInfo = userInfo;
				} else {
					this.userInfo = {};
					this.userInfo.loginStatus = false;
				}
			},
			submitUpdateUserInfo() {
				if (!this.upUser.nickname) {
					uni.showToast({
						title: '昵称不能为空',
						icon: 'none',
						duration: 1500
					});
					return;
				}
				// 调用接口更新个人信息
				this.upUser.id = this.userInfo.id;
				this.$http.post({
					url: apis.updateMineInfo,
					data: this.upUser,
				}).then(res => {
					if (res.code === 200) {
						this.getUserInfo();
						this.handleCloseUpdate();
						uni.showToast({
							title: '修改成功',
							icon: 'none',
							duration: 1500
						});
					}
				}).catch(err => {
					console.log('调用接口失败：', err);
				});
			},
			goPage(index) {
				const token = uni.getStorageSync('token');
				if (token && this.userInfo.loginStatus) {
					if (index === 0) {
						uni.navigateTo({
							url: '/pages/purse/purse'
						});
					} else if (index === 1) {
						uni.navigateTo({
							url: '/pages/activity/activity'
						});
					} else if (index === 2) {
						uni.navigateTo({
							url: '/pages/shop/shop'
						});
					} else if (index === 4) {
						uni.navigateTo({
							url: '/pages/order/order'
						});
					} else if (index === 5) {
						// 复制UID
						const userInfo = uni.getStorageSync('userInfo');
						if (userInfo && userInfo.account) {
							uni.setClipboardData({
								data: userInfo.account,
								success: function () {
									console.log('success');
								}
							});
						}
					}
				} else {
					uni.showToast({
						title: '请先登录',
						icon: 'none',
						duration: 1500
					});
					this.$refs.loginDialog.open();
				}
			}
		}
	}
</script>

<style lang="scss">
	
	.mine-base-style {
		margin: 10rpx 0;
		background-color: #ffffff;
		padding: 20rpx;
		box-shadow: 0 2px 8px 0 rgba(0,0,0,0.2);
	}
	
	.mine-wrap {

		/* 顶部背景图 start */
		.top-backgroup {
			height: 380rpx;
			box-shadow: 0 2px 8px 0 rgba(0,0,0,0.2);

			.backgroud-image {
				width: 100%;
				height: 380rpx;
				// z-index: -1;
			}

			.mine-user-info {
				position: absolute;
				top: 0;
				left: 0;


				.top-space {
					height: 200rpx;
					width: 100%;
				}

				.mine-user-bar {
					display: flex;
					align-items: center;
					margin: 0 40rpx;

					&-avatar {
						margin-right: 20rpx;

						>image {
							width: 140rpx;
							height: 140rpx;
							border-radius: 50%;
						}
					}

					&-name {

						.mine-user-bar-nick {
							color: #333333;
							font-size: 34rpx;
						}

						.mine-user-bar-uid {
							font-size: 28rpx;
							color: #999999;
							margin-top: 20rpx;
						}

					}

				}
			}

		}
		
		.mine-menus {
			
			.mine-menus-item {
				display: flex;
				align-items: center;
				justify-content: space-between;
				margin-top: 30rpx;
				
				&-title {
					font-size: 32rpx;
				}
				
				&-icon {
					width: 48rpx;
					height: 48rpx;
					
					> image {
						width: 48rpx;
						height: 48rpx;
					}
				}
			}
			
			.mine-menus-item:first-child {
				margin-top: 0;
			}
		}
		
		.mine-gzh-adv {
			display: flex;
			align-items: center;
			
			.mine-gzh-code {
				margin-right: 20rpx;
				
				> image {
					width: 160rpx;
					height: 160rpx;
				}
			}
			
			.mine-gzh-title {
				
			}
			
		}
		
		.mine-update-user-model {
			background: #ffffff;
			width: 70vw;
			height: 30vh;
			border-radius: 20rpx;
			padding: 20rpx;
			position: relative;
			display: flex;
			flex-flow: column;
			align-items: center;
			opacity: 1;
			
			.update-model-close {
				position: absolute;
				right: 20rpx;
				top: 20rpx;
				width: 40rpx;
				height: 40rpx;
			}
			
			.mine-update-title {
				font-size: 32rpx;
				border-bottom: 1px solid #f8f8f8;
				padding-bottom: 20rpx;
				width: 100%;
			}
			
			.mine-update-body {
				flex: 1;
				display: flex;
				align-items: center;
				
				.uni-form-item {
					display: flex;
					align-items: center;
					
					> input {
						border: 1px solid #f2f2f2;
						padding: 10rpx;
						border-radius: 10rpx;
					}
				}
			}
			
			.mine-update-btns {
				display: flex;
				align-items: center;
				justify-content: center;
				height: 80rpx;
			}
		}
		
		.mine-login-model {
			background: #ffffff;
			width: 60vw;
			height: 30vh;
			border-radius: 20rpx;
			padding: 20rpx;
			display: flex;
			flex-flow: column;
			justify-content: center;
			align-items: center;
			position: relative;
			
			> image {
				width: 200rpx;
				height: 200rpx;
				margin-bottom: 20rpx;
			}
			
			.login-model-close {
				position: absolute;
				right: 20rpx;
				top: 20rpx;
				width: 40rpx;
				height: 40rpx;
			}
		}
	}
</style>